// pages/compandz/companydz.js
const app = getApp()

Page({

  data: {
    imgSrc: [],
    bgattr: [],
    topattr: ['', '', '', '', '', ''],
    image: '',
  },

  onLoad: function(options) {
    this.getimgs()
  
  },
  onShow() {
    this.getimgs()
  },
  getimgs() { //获取基本信息
    let that = this;
    wx.request({ //传后台
      url: app.globalData.url + '/jxb/egoods/egoods/getEsonorder',
      method: 'get',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        customer: wx.getStorageSync('customer'),
        token: wx.getStorageSync('token'),
      },
      success: function(res) {
        if (res.data.code == 0) {
          console.log(res.data.data)
          if (res.data.data) {
            that.setData({
              imgSrc: res.data.data
            })
            that.checknum();
          }
        } else {
          // wx.showToast({
          //   title: res.data.msg,
          //   icon: 'none'
          // });
          // wx.navigateTo({
          //   url: '/pages/company/company',
          // })
        }
      },

    })
  },


  photos() { //上传图片
    console.log(wx.getStorageSync('customer'))
    if (wx.getStorageSync('customer')){
      wx: wx.navigateTo({
        url: '/pages/upimg/upimg?type=company',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }else{
      wx:wx.showToast({
        title: '请填写公司信息',
        icon: 'none',
      })
    }
    
    return;
    let that = this;
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          title: '上传中...',
        })
        wx.uploadFile({
          url: app.globalData.url + '/jxb/goods/goods/uploadPic', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'files',
          formData: {
            'user': 'test'
          },
          success(res) {
            wx.showToast({
              title: '上传成功',
            })
            let data = JSON.parse(res.data)
            let obj = {};
            obj.url = data.data.url;
            obj.pid = data.data.pid;
            let imgSrc = that.data.imgSrc;
            let topattr = that.data.topattr;
            let bgattr = that.data.bgattr;
            imgSrc.push(obj);
            if (imgSrc.length == 1) {
              topattr = ['A', 'A', 'A', 'A', 'A', 'A', ];
              bgattr = ['A']
            } else if (imgSrc.length == 2) {
              topattr = ['A', 'A', 'A', 'B', 'B', 'B', ];
              bgattr = ['A', 'B']
            } else if (imgSrc.length == 3) {
              topattr = ['A', 'A', 'B', 'B', 'C', 'C', ];
              bgattr = ['A', 'B', 'C']
            } else if (imgSrc.length == 6) {
              topattr = ['A', 'B', 'C', 'D', 'E', 'F', ];
              bgattr = ['A', 'B', 'C', 'D', 'E', 'F', ]
            } else if (imgSrc.length == 4) {
              topattr = ['A', 'B', 'C', 'D', '', '', ];
              bgattr = ['A', 'B', 'C', 'D', ]
            } else if (imgSrc.length == 5) {
              topattr = ['A', 'B', 'C', 'D', 'E', '', ]
              bgattr = ['A', 'B', 'C', 'D', 'E']
            }
            that.setData({
              imgSrc: imgSrc,
              topattr: topattr,
              bgattr: bgattr,
            })
          }
        })
      }
    })
  },

  checknum() {
    let imgSrc = this.data.imgSrc;
    let topattr = this.data.topattr;
    let bgattr = this.data.bgattr;
    if (imgSrc.length) {
      if (imgSrc.length == 1) {
        topattr = ['A', 'A', 'A', 'A', 'A', 'A', ];
        bgattr = ['A']
      } else if (imgSrc.length == 2) {
        topattr = ['A', 'A', 'A', 'B', 'B', 'B', ];
        bgattr = ['A', 'B']
      } else if (imgSrc.length == 3) {
        topattr = ['A', 'A', 'B', 'B', 'C', 'C', ];
        bgattr = ['A', 'B', 'C']
      } else if (imgSrc.length == 6) {
        topattr = ['A', 'B', 'C', 'D', 'E', 'F', ];
        bgattr = ['A', 'B', 'C', 'D', 'E', 'F', ]
      } else if (imgSrc.length == 4) {
        topattr = ['A', 'B', 'C', 'D', '', '', ];
        bgattr = ['A', 'B', 'C', 'D', ]
      } else if (imgSrc.length == 5) {
        topattr = ['A', 'B', 'C', 'D', 'E', '', ]
        bgattr = ['A', 'B', 'C', 'D', 'E']

      }
      this.setData({
        topattr: topattr,
        bgattr: bgattr,
      })
    }
  },
  deleteImage: function(e) { //删除图片
    var that = this;
    console.log(e.currentTarget.dataset.item);
    let item = e.currentTarget.dataset.item
    var index = e.currentTarget.dataset.index; //获取当前长按图片下标
    let imgSrc = this.data.imgSrc;
    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success: function(res) {
        if (res.confirm) {
          wx.showLoading();
          wx.request({ //传后台
            url: app.globalData.url + '/jxb/egoods/egoods/delPicText',
            method: 'get',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              customer: wx.getStorageSync('customer'),
              pidurl: wx.getStorageSync('token'),
              pidiurl: item.pidiurl,
              loan: item.loan,
              quotation: item.quotation,
              position: item.position,
              tcolor: item.tcolor,
              epid: item.epid,
            },
            success: function(res) {
              if (res.data.code == 0) {

                wx.showToast({
                  title: '删除成功',
                })
                imgSrc.splice(index, 1)
                that.setData({
                  imgSrc: imgSrc,
                })
                that.checknum()
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                })
              }
            },
            complete: function() {
              wx.hideLoading();
            }

          })
        } else if (res.cancel) {
          console.log('点击取消了');
          return false;
        }
      }
    })
  },
  seeview(e){//预览图片
    let item = e.currentTarget.dataset.item
    var index = e.currentTarget.dataset.index; 
    wx.previewImage({
      urls: [item.pidiurl],
    })
  },
  addadress() {
    if (this.data.imgSrc.length == 4 || this.data.imgSrc.length == 5 || this.data.imgSrc.length == 0) {
      wx.showToast({
        title: '上传图片数量应为6的整除数',
        icon: 'none'
      })
    } else {
      wx.navigateTo({
        url: '/pages/companypay/companypay'
      })
    }
  }
})